//
//  MyImage.m
//  MyCloud
//
//  Created by Visitor on 8/4/13.
//  Copyright (c) 2013 visitor.zc@gmail.com. All rights reserved.
//

#import "MyImage.h"
#import <AssetsLibrary/AssetsLibrary.h>

@implementation MyImage
{
    NSMutableArray *_images;
}
@synthesize delegate = _delegate;

- (void)dealloc
{
    [_images release];
    [super dealloc];
}

- (id)init
{
    self = [super init];
    if (self) {
        _images = [[NSMutableArray alloc] init];
    }
    return self;
}

static MyImage *_sharedMyImage;

+ (MyImage *)sharedMyImage
{
    if(!_sharedMyImage)
    {
        _sharedMyImage = [[MyImage alloc] init];
    }
    return _sharedMyImage;
}

- (void)getImageList
{
    NSMutableArray *imageArray = [[NSMutableArray alloc] init];
    NSMutableArray *groupNames = [[NSMutableArray alloc] init];
    
    dispatch_async(dispatch_get_main_queue(),^{
        
        NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    
        ALAssetsLibraryAccessFailureBlock failureblock = ^(NSError *myerror)
        {
            NSLog(@"相册访问失败 =%@", [myerror localizedDescription]);
            if ([myerror.localizedDescription rangeOfString:@"Global denied access"].location!=NSNotFound)
            {
                NSLog(@"无法访问相册.请在'设置->定位服务'设置为打开状态.");
            }
            else
            {
                NSLog(@"相册访问失败.");
            }
        };
        

        ALAssetsGroupEnumerationResultsBlock groupEnumerAtion = ^(ALAsset *result, NSUInteger index, BOOL *stop)
        {
            if (result!=NULL) {
                
                if ([[result valueForProperty:ALAssetPropertyType] isEqualToString:ALAssetTypePhoto])
                {
                    //图片的url
                    NSString *urlstr=[NSString stringWithFormat:@"%@",result.defaultRepresentation.url];
                    /*
                     //图片的大图
                     result.defaultRepresentation.fullScreenImage
                     //图片的缩略图小图
                     result.thumbnail                             
                    //                    NSRange range1=[urlstr rangeOfString:@"id="];
                    //                    NSString *resultName=[urlstr substringFromIndex:range1.location+3];
                    //                    resultName=[resultName stringByReplacingOccurrencesOfString:@"&ext=" withString:@"."];//格式demo:123456.png
                    */
                    NSLog(@"url = %@",urlstr);
                    [imageArray addObject:urlstr];
                }
            }
            [_delegate getImageListFinish:imageArray];
            
        };
        
        ALAssetsLibraryGroupsEnumerationResultsBlock
        libraryGroupsEnumeration = ^(ALAssetsGroup* group, BOOL* stop)
        {
            
            if (group == nil)
            {
                
            }
            if (group!=nil)
            {
                //获取相簿的组
                NSString *g=[NSString stringWithFormat:@"%@",group];
                //gg:ALAssetsGroup - Name:Camera Roll, Type:Saved Photos, Assets count:71
                NSLog(@"gg:%@",g);
                
                NSString *g1=[g substringFromIndex:16] ;
                NSArray *arr=[[NSArray alloc] init];
                arr=[g1 componentsSeparatedByString:@","];
                //NSLog(@"arr = %@",arr);
                NSString *g2=[[arr objectAtIndex:0] substringFromIndex:5];
                if ([g2 isEqualToString:@"Camera Roll"])
                {
                    g2=@"相机胶卷";
                }
                //组的name
                NSString *groupName=g2;
                [groupNames addObject:arr];
                [group enumerateAssetsUsingBlock:groupEnumerAtion];
            }
            [_delegate getImageGroupFinish:groupNames];
        };
        
        ALAssetsLibrary* library = [[ALAssetsLibrary alloc] init];
        [library enumerateGroupsWithTypes:ALAssetsGroupAll
                               usingBlock:libraryGroupsEnumeration
                             failureBlock:failureblock];
        [library release];
        [pool release];
    });
}


- (void)getImageWithStr:(NSString *)urlStr;
{
    ALAssetsLibrary *assetLibrary=[[ALAssetsLibrary alloc] init];
    NSURL *url=[NSURL URLWithString:urlStr];
    [assetLibrary assetForURL:url resultBlock:^(ALAsset *asset)
    {
        // 得到的图片
        UIImage *image = [UIImage imageWithCGImage:asset.thumbnail];
        [_images addObject:image];
        [_delegate getImageFinish:image];
    }
                 failureBlock:^(NSError *error)
    {
        NSLog(@"error=%@",error);
    }];
}
    


@end
